package com.hui.controller;

import com.hui.enums.YesOrNo;
import com.hui.pojo.Carousel;
import com.hui.pojo.Category;
import com.hui.pojo.vo.CategoryVO;
import com.hui.pojo.vo.NewItemsVO;
import com.hui.service.CarouselService;
import com.hui.service.CategoryService;
import com.hui.utils.MyJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "首页",tags = {"首页展示相关实现"})
@RestController
@RequestMapping("index")
public class IndexController {
    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;
    public static final Logger log =
            LoggerFactory.getLogger(IndexController.class);

    @ApiOperation(value = "首页轮播图",notes = "查询所有",httpMethod = "GET")
    @GetMapping("/carousel")
    public MyJSONResult carousel(){
        List<Carousel> carouselList = carouselService.queryAll(YesOrNo.Yes.type);
        return MyJSONResult.ok(carouselList);
    }
    /**
     * 首页分类展示需求
     * 1。 第一次刷新诸恶业 查询大分类 渲染展示到首页
     * 2 如果鼠标上移到打分类  则加载子分类内容  已经缓存则不再加载
     */
    @ApiOperation(value = "一级分类",notes = "获取商品一级分类",httpMethod = "GET")
    @GetMapping("/cats")
    public MyJSONResult cats(){
        List<Category> carouselList = categoryService.queryAllRootLevelCat();
        return MyJSONResult.ok(carouselList);
    }

    @ApiOperation(value = "二级分类",notes = "获取商品二级分类",httpMethod = "GET")
    @GetMapping("/subCat/{rootCateId}")
    public MyJSONResult subCat(
            @ApiParam(name = "rootCateId",value = "一级分类Id",required = true)
            @PathVariable Integer rootCateId){
        if (rootCateId==null){
            return MyJSONResult.errorMsg("无效操作");
        }
        List<CategoryVO> carouselList = categoryService.getSubCatList(rootCateId);
        return MyJSONResult.ok(carouselList);
    }


    @ApiOperation(value = "查询每个一级分类下的最新6条商品数据", notes = "查询每个一级分类下的最新6条商品数据", httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public MyJSONResult sixNewItems(
            @ApiParam(name = "rootCatId", value = "一级分类id", required = true)
            @PathVariable Integer rootCatId) {

        if (rootCatId == null) {
            return MyJSONResult.errorMsg("分类不存在");
        }

        List<NewItemsVO> list = categoryService.getSixNewItemsLazy(rootCatId);
        return MyJSONResult.ok(list);
    }
}
